﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FOModels.Articles;
using DataAccess.Articles;
using System.Data;
using System.Net;
namespace Bussiness.Articles
{
    public class ArticlesBLL
    {
        private ArticlesDAL dal;
        public ArticlesBLL()
        {
            dal = new ArticlesDAL();
        }
        public ArticlesModels getTOP1()
        {
            ArticlesModels fur = new ArticlesModels();
            DataTable tb = dal.getTOP1();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.ArticlesID = int.Parse(row[0].ToString());
                }
                return fur;
            }
            else
            {
                return null;
            }

        }
        public int UpdateArticles(ArticlesModels models, int langid)
        {
            if (dal.UpdateArticles(models) > 0 && dal.UpdateArticlesLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertArticles(ArticlesModels models)
        {
            if (dal.InsertArticles(models) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertArticlesLanng(ArticlesModels models,int langid)
        {
            if (dal.InsertArticlesLang(models,langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeleteArticles(int CarLoanID)
        {
            if (dal.DeleteArticles(CarLoanID) > 0 && dal.DeleteArticlesLang(CarLoanID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public List<ArticlesCateModels> getArticlesCate(int langid)
        {
            List<ArticlesCateModels> list = new List<ArticlesCateModels>();
            ArticlesCateModels fur;
            DataTable tb = dal.getArticlesCate(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new ArticlesCateModels();
                    fur.ArticlesCateID = int.Parse(row[0].ToString());
                    fur.ArticlesCateName = row[4].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public ArticlesModels getArticlesByID(int ArticlesID, int langid)
        {
            ArticlesModels fur = new ArticlesModels();
            DataTable tb = dal.getArticlesByID(ArticlesID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.ArticlesID = int.Parse(row[0].ToString());
                    fur.Images = row[1].ToString();
                    fur.UserPost = row[2].ToString();
                    fur.DatePost = row[3].ToString();
                    fur.UserUpdate = row[4].ToString();
                    fur.DateUpdate = row[5].ToString();
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.ArticlesCateID = int.Parse(row[7].ToString());
                    fur.Title = row[10].ToString();
                    fur.Summary = row[11].ToString();
                    fur.Content = WebUtility.HtmlDecode(row[12].ToString());
                }
                return fur;
            }
            else
            {
                return null;
            }

        }
        public ArticlesModels getArticlesByIDShow(int ArticlesID, int langid)
        {
            ArticlesModels fur = new ArticlesModels();
            DataTable tb = dal.getArticlesByIDShow(ArticlesID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.ArticlesID = int.Parse(row[0].ToString());
                    fur.Images = row[1].ToString();
                    fur.UserPost = row[2].ToString();
                    fur.DatePost = row[3].ToString();
                    fur.UserUpdate = row[4].ToString();
                    fur.DateUpdate = row[5].ToString();
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.ArticlesCateID = int.Parse(row[7].ToString());
                    fur.Title = row[10].ToString();
                    fur.Summary = row[11].ToString();
                    fur.Content = WebUtility.HtmlDecode(row[12].ToString());
                }
                return fur;
            }
            else
            {
                return null;
            }

        }
        public List<ArticlesModels> getArticlesByCateIDShow(int ArticlesCateID, int langid)
        {
            List<ArticlesModels> list = new List<ArticlesModels>();
            ArticlesModels fur;
            DataTable tb = dal.getArticlesByCateIDShow(ArticlesCateID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new ArticlesModels();
                    fur.ArticlesID = int.Parse(row[0].ToString());
                    fur.Images = row[1].ToString();
                    fur.UserPost = row[2].ToString();
                    fur.DatePost = row[3].ToString();
                    fur.UserUpdate = row[4].ToString();
                    fur.DateUpdate = row[5].ToString();
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.ArticlesCateID = int.Parse(row[7].ToString());
                    fur.Title = row[10].ToString();
                    fur.Summary = row[11].ToString();
                    fur.Content = WebUtility.HtmlDecode(row[12].ToString());
                    list.Add(fur);
                }
              
            }
            return list;

        }
        public List<ArticlesModels> getall(int langid)
        {
            List<ArticlesModels> list = new List<ArticlesModels>();
            ArticlesModels fur;
            DataTable tb = dal.getall(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new ArticlesModels();
                    fur.ArticlesID = int.Parse(row[0].ToString());
                    fur.Images = row[1].ToString();
                    fur.UserPost = row[2].ToString();
                    fur.DatePost = row[3].ToString();
                    fur.UserUpdate = row[4].ToString();
                    fur.DateUpdate = row[5].ToString();
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.ArticlesCateID = int.Parse(row[7].ToString());
                    fur.Title = row[10].ToString();
                    fur.Summary = row[11].ToString();
                    fur.Content = WebUtility.HtmlDecode(row[12].ToString());
                    fur.ArticlesCateName = row[17].ToString();
                    fur.FlagImage = row[21].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
    }

}

